"""
 This module mainly exists for the MySpace Bands And Celebs demo
 When the user requests http://<hostname>/celebs, it sets the user name as "entertainment"
 and sets up a subscription via the following callback url: 
 http://<hostname>/subscriber.celebrities

"""

import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import simplejson
import gmemsess
import ckeynsecret

from myspace.myspaceapi import MySpace
from usersession import User
from models import Subscription, RawFeed

class CelebsHandler(webapp.RequestHandler):
    user_id = "entertainment"
    
    def get(self):
        self.show_main_page()

    def show_main_page(self):
        self.check_subscription()                

        session=gmemsess.Session(self)
        User.set_current_user(self, self.user_id, self.user_id)

        template_data = {
                'user_name' : self.user_id,
        }        
        path = os.path.join(os.path.dirname(__file__), 'templates/updates.html')
        return self.response.out.write(template.render(path, template_data))
    
    """ Checks to see if a subscription has been setup or not
        Makes a subscription request if one's not already present in the datastore
    """
    def check_subscription(self):
        subscription = Subscription.get_user_subscription(self.user_id)
        if subscription:
            return True
        else:
            # Used when reading the celeb list from a file
            #infile = open('entertainment_subscription.xml')
            #subscription_payload = infile.read()
            
            # Used for a subset of hardcoded celeb ids
            callback_url = self.request.host_url + "/subscriber." + self.user_id
            subscription_payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
                                "<Subscription>"\
                                "<Type>UserList</Type>" \
                                "<Endpoint><![CDATA[" + callback_url + "]]></Endpoint>"\
                                "<Query></Query>"\
                                "<MetaData>UserInfo,UserSubscribers,ApplicationData</MetaData>"\
                                "<BatchSize>1</BatchSize>"\
                                "<Rate>100</Rate>"\
                                "<Format><![CDATA[application/atom+xml]]></Format>"\
                                "<UserList>"\
                                "  <AddList>"\
                                "    <UserId>296768296</UserId>"\
                                "    <UserId>106078907</UserId>"\
                                "    <UserId>74211693</UserId>"\
                                "    <UserId>184040237</UserId>"\
                                "    <UserId>415461193</UserId>"\
                                "    <UserId>20810882</UserId>"\
                                "    <UserId>2733826</UserId>"\
                                "    <UserId>26646019</UserId>"\
                                "    <UserId>9347892</UserId>"\
                                "    <UserId>40137611</UserId>"\
                                "    <UserId>41736</UserId>"\
                                "  </AddList>"\
                                "</UserList>"\
                                "</Subscription>"
            return self.perform_subscription(subscription_payload)
    
    def perform_subscription(self, query_xml):
        ms = MySpace(ckeynsecret.CONSUMER_KEY, ckeynsecret.CONSUMER_SECRET)
        status,subscription_id = ms.register_subscription(query_xml)
        if status == 201:
           subscription = Subscription(user=self.user_id, ms_subscription_id=subscription_id)
           subscription.put()
           